﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;

namespace TinyEngine.Editor.Extensibility.Controls
{
    /// <summary>
    /// Interaction logic for VectorControl.xaml
    /// </summary>
    public partial class VectorControl : UserControl
    {
        bool isVector3;
        public VectorControl(string name)
        {
            InitializeComponent();
            lblName.Text = name;
        }
        private Vector3 vector;
        public Vector3 Vector 
        {
            get { return vector; }
            set
            {
                vector = value;

                txtX.Text = vector.X.ToString();
                txtY.Text = vector.Y.ToString();
                txtZ.Text = vector.Z.ToString();
                isVector3 = true;
                txtZ.Visibility = lblZ.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private Vector2 vector2;
        public Vector2 Vector2
        {
            get { return vector2; }
            set
            {
                vector2 = value;

                txtX.Text = vector2.X.ToString();
                txtY.Text = vector2.Y.ToString();
                txtZ.Visibility = lblZ.Visibility = System.Windows.Visibility.Collapsed;
                isVector3 = false;
            }
        }
        public event Action VectorChanged;

        private void txtX_TextChanged(object sender, TextChangedEventArgs e)
        {
            var txtValX = txtX.Text;
            var txtValY = txtY.Text;
            var txtValZ = txtZ.Text;

            float valX, valY, valZ = 0.0f;

            if (float.TryParse(txtValX, out valX))
            {
                if (isVector3)
                    vector.X = valX;
                else
                    vector2.X = valX;
            }

            if (float.TryParse(txtValY, out valY))
            {
                if (isVector3)
                    vector.Y = valY;
                else
                    vector2.Y = valY;
            }

            if (float.TryParse(txtValZ, out valZ))
            {
                if (isVector3)
                    vector.Z = valZ;
            }
            
            if(VectorChanged!=null)
                VectorChanged();
        }

    }
}
